// pages/annualPerson/annualPerson.js
Page({
  data: {
    annualChildEduBox:[1],
    annualChildEduStart:['子女入学时间'],
    annualChildEduEnd:['终止教育时间'],
    annualChildEduRatioChoice:{choiceHalf:true,choiceWhole:false},
    annualChildEduRatioValue:0.5,

    annualFurtherEduChoice:{yes:false,no:true},

    annualDegreeChoice:{yes:false,no:true},
    annualDegreeStart:"请选择入学时间",
    annualDegreeEnd:"预计毕业时间",
    annualDegreeValue:0,

    annualBabyBox:[1],
    annualBabyBirth:['请选择出生年月'],
    annualBabyCareRatioValue:0.5,
    annualBabyChoice:{choiceHalf:true,choiceWhole:false},

    annualRentBox:[], 
    annualRentStart:['房租开始时间'], 
    annualRentEnd:['房租到期时间'], 
    annualRentType:[], 
    annualRentChoice:[{cityA:false,cityB:false,cityC:false,noneCity:true}],
    
    annualLoanNum:0,
    annualLoanChoice:{half:false,whole:false,noneLoan:true},
    annualLoanEndDate:"贷款还完时间",
    annualLoanStartDate:"首次还款时间",
    
     
    annualElderDate:"请选择老人何时年满六十",
    annualElderChoice:{choice1:false,choice2:false,choice3:false,choice4:false,choice5:false,
      choice6:false,choice7:false,choice8:false,choice9:false,choice10:false,choice0:true},
      annualElderValue:0,
    
    nowYear:"",
    totalAnnualPersonCount:'0.00',
    annualBabyBond:"",

    fold:false,
    fold1:false,
    fold2:false,
    fold3:false,
    fold4:false,
    fold5:false,
    fold6:false,
    
    componentCount:{
      childEdu:0,
      furEdu:0,
      degree:0,
      loan:0,
      rent:0,
      babyCare:0,
      elder:0
    },
  },

  // 将当前7项专项附加扣除的值加在一起
  calSumResult:function(){
    var com = this.data.componentCount;
    var result = 0;
    for(var key in com){
      result = result + Number(com[key]);
    }
    result = result.toFixed(2)
    this.setData({totalAnnualPersonCount:result.toString()});
  },

  toggleFold(e){
    this.setData({fold:!this.data.fold})
  },

  toggleFold1(e){
    this.setData({fold1:!this.data.fold1})
  },

  toggleFold2(e){
    this.setData({fold2:!this.data.fold2})
  },
  toggleFold3(e){
    this.setData({fold3:!this.data.fold3})
  },
  toggleFold4(e){
    this.setData({fold4:!this.data.fold4})
  },
  toggleFold5(e){
    this.setData({fold5:!this.data.fold5})
  },

  toggleFold6(e){
    this.setData({fold6:!this.data.fold6})
  },

  // 比较两个日期大小
  compareDate:function(dateStr1,dateStr2){
    const date1 = new Date(dateStr1);
    const date2 = new Date(dateStr2);
    if (date1 > date2) {
        return 1;
    } else if (date1 < date2) {
        return 2;
    } else {
        return 0;
    }
  },

/**
 * 根据输入的开始日期字符串和结束字符串，判断输入的日期在本年度共占几个月
 * @param {String} startStr 
 * @param {String} endStr 
 */
calMonthInThisYear:function(startStr,endStr){
  var end = endStr;
  var start = startStr;
  var calStart = this.data.nowYear;
  calStart = calStart.concat("-01")
  var calEnd =  this.data.nowYear;
  calEnd = calEnd.concat("-12");
  var result = 0;
  //传入其他字符串，直接返回0
  if(end.indexOf("-") === -1 || start.indexOf("-") === -1){
    return 0;
  }
  var startComCal = this.compareDate(calStart,start);
  var calComEnd = this.compareDate(calEnd,end);
  var endComYearStart = this.compareDate(calStart,end);
  var startComYearEnd = this.compareDate(calEnd,start);
  // start > 本年12月
  if(startComYearEnd == 2){
    return -1;
  }   
  if(startComCal <= 1){
    //本年12月 < end,完整包含整个本年度，直接返回12月
    if(calComEnd === 2)result = 12;
    //本年1月<end<本年12月
    else if(endComYearStart === 2 && calComEnd <= 1){
      result = Number(end.slice(-2))
    }
  }
  // 当start大于本年1月
  else if(startComCal === 2)
  {
    // end小于calEnd
    if(calComEnd <= 1){
      result = Number(end.slice(-2)) - Number(start.slice(-2)) + 1;
    }
    // end大于本年12月
    else{
      result = 12 - Number(start.slice(-2)) + 1;
    }
  }
  return result;
},

  // 判断当前索引月份是否在当前时间区间内
  isMonthInThatDuration:function(startStr,endStr,monthIdx){
    var year = this.data.nowYear;
    var start = startStr;
    var end = endStr;
    //结束年份小于纳税年份，返回为false
    if(Number(end.slice(0,4)) < Number(year)){
       return false;
    }
    // 结束年份等于或大于当前计算年份
    else{
      if(Number(start.slice(0,4)) < Number(year)){
        if(Number(end.slice(-2)) > monthIdx){
           return true;
        }
      }else if(Number(start.slice(0,4)) === Number(year)){
        if(Number(end.slice(-2)) >= monthIdx && Number(start.slice(-2)) <= monthIdx){
          return true;
       }
      }else;
    }
    return false;
  },

  //计算dateTime与calDate中间差几个月，跨年度的话就算1月到calDate的月份差
  calMonthMinus:function(dateTime){ 
    //没填的直接返回0
    if(dateTime.indexOf("-") === -1){
      return 0;
    }
    var calDate = this.data.nowYear; 
    calDate = calDate+"-12"
    var tempDate = dateTime; 
    var tempSplit = tempDate.split("-"); 
    var calSplit = calDate.split("-"); 
    var minus = 0; 
    if(Number(tempSplit[0]) < Number(calSplit[0])){ 
      //年份小于纳税年份，直接返回1月到需要计算月份的月份差 
      minus = Number(calSplit[1]) 
    // 月份大于纳税月份，也就是在未来年份报的，直接忽略
    }else if(Number(tempSplit[0]) > Number(calSplit[0])){ 
      minus = 0;
    }else{
      // 年份相同的情况下,月份大于纳税期，返回0
      if(Number(tempSplit[1]) > Number(calSplit[1])){
        minus = 0;
      }else{
        minus = Number(calSplit[1]) - Number(tempSplit[1]) + 1;
      }
    }
    return minus; 
  }, 

  // 计算房租累计专项附加扣除
calRentKouchu:function(){
  var rent = this.data.annualRentType;
  var start = this.data.annualRentStart;
  var end = this.data.annualRentEnd;
  var rentBox = this.data.annualRentBox;
  var result = 0;
  //因为年度汇算，所以判断12个月的
  var monthLength = 12;
  for(var i = 1;i <= monthLength;i++){
    // console.log("i的取值:"+i)
   for(var j = 0;j < rentBox.length;j++){
     if(this.isMonthInThatDuration(start[j],end[j],i)){
       result = result + rent[j];
       break;
     }else{
       result = result + 0;
     }    
   }
  }
  this.setData({"componentCount.rent":result});
},

  insertEdu(e){
    var cb = this.data.annualChildEduBox;
    var start = this.data.annualChildEduStart;
    var end = this.data.annualChildEduEnd;
    cb.push(cb.length+1);
    start.push('请选择入学时间');
    end.push('终止教育时间')
    this.setData({annualChildEduBox: cb,annualChildEduStart:start,annualChildEduEnd:end});
  },

  delEdu(e){
    var cb = this.data.annualChildEduBox;
    var start = this.data.annualChildEduStart;
    var end = this.data.annualChildEduEnd;
    cb.pop();
    start.pop();
    end.pop();
    this.setData({annualChildEduBox: cb,annualChildEduStart:start,annualChildEduEnd:end});
  },

  insertBaby(e){
    var bb = this.data.annualBabyBox;
    var birth = this.data.annualBabyBirth;
    bb.push(bb.length+1);
    birth.push('请选择出生年月');
    this.setData({annualBabyBox: bb,annualBabyBirth:birth});
  },

  delBaby(e){
    var bb = this.data.annualBabyBox;
    var birth = this.data.annualBabyBirth;
    bb.pop();
    birth.pop();
    this.setData({annualBabyBox: bb,annualBabyBirth:birth});
  },

  insertRent(e){ 
    var rb = this.data.annualRentBox; 
    var rs = this.data.annualRentStart; 
    var re = this.data.annualRentEnd; 
    var type = this.data.annualRentType; 
    var rentChoice = this.data.annualRentChoice;
    rb.push(rb.length+1); 
    rs.push('房租开始时间'); 
    re.push('房租到期时间'); 
    rentChoice.push({cityA:false,cityB:false,cityC:false,noneCity:true})
    type.push(0); 
    this.setData({annualRentBox:rb,annualRentStart:rs,annualRentEnd:re,annualRentType:type,annualRentChoice:rentChoice}); 
  }, 
 
  delRent(e){ 
    var rb = this.data.annualRentBox; 
    var rs = this.data.annualRentStart; 
    var re = this.data.annualRentEnd; 
    var type = this.data.annualRentType; 
    var rentChoice = this.data.annualRentChoice;
    rb.pop(); 
    rs.pop(); 
    re.pop(); 
    rentChoice.pop();
    type.pop()
    this.setData({annualRentBox:rb,annualRentStart:rs,annualRentEnd:re,annualRentType:type,annualRentChoice:rentChoice}); 
  }, 
  
  //单选框确定扣除标准
  elderChange(e){
    var val = e.detail.value;
    val = Number(val);
    var date = this.data.annualElderDate;
    var elderChoice = this.data.annualElderChoice;
    
    if(date.indexOf("-") === -1 && val !== 0){
      wx.showToast({
        title: '请输入老人何时年满六十！',
      });
      elderChoice.choice0 = true;
      elderChoice.choice1 = false;
      elderChoice.choice2 = false;
      elderChoice.choice3 = false;
      elderChoice.choice4 = false;
      elderChoice.choice5 = false;
      elderChoice.choice6 = false;
      elderChoice.choice7 = false;
      elderChoice.choice8 = false;
      elderChoice.choice9 = false;
      elderChoice.choice10 = false;
      this.setData({annualElderChoice:elderChoice});
      this.setData({annualElderValue:0});
      return;
    }
    if(val === 3000){
      elderChoice.choice0 = false;
      elderChoice.choice1 = true;
      elderChoice.choice2 = false;
      elderChoice.choice3 = false;
      elderChoice.choice4 = false;
      elderChoice.choice5 = false;
      elderChoice.choice6 = false;
      elderChoice.choice7 = false;
      elderChoice.choice8 = false;
      elderChoice.choice9 = false;
      elderChoice.choice10 = false;
      this.setData({annualElderValue:val});
      this.setData({annualElderChoice:elderChoice});
      return;
    }else if(val === 1500){
      elderChoice.choice0 = false;
      elderChoice.choice1 = false;
      elderChoice.choice2 = true;
      elderChoice.choice3 = false;
      elderChoice.choice4 = false;
      elderChoice.choice5 = false;
      elderChoice.choice6 = false;
      elderChoice.choice7 = false;
      elderChoice.choice8 = false;
      elderChoice.choice9 = false;  
      elderChoice.choice10 = false;
      this.setData({annualElderValue:val});
      this.setData({annualElderChoice:elderChoice});
      return;
    }else if(val === 1000){
      elderChoice.choice0 = false;
      elderChoice.choice1 = false;
      elderChoice.choice2 = false;
      elderChoice.choice3 = true;
      elderChoice.choice4 = false;
      elderChoice.choice5 = false;
      elderChoice.choice6 = false;
      elderChoice.choice7 = false;
      elderChoice.choice8 = false;
      elderChoice.choice9 = false;
      elderChoice.choice10 = false;
      this.setData({annualElderValue:val});
      this.setData({annualElderChoice:elderChoice});
      return;
    }else if(val === 750){
      elderChoice.choice0 = false;
      elderChoice.choice1 = false;
      elderChoice.choice2 = false;
      elderChoice.choice3 = false;
      elderChoice.choice4 = true;
      elderChoice.choice5 = false;
      elderChoice.choice6 = false;
      elderChoice.choice7 = false;
      elderChoice.choice8 = false;
      elderChoice.choice9 = false;
      elderChoice.choice10 = false;
      this.setData({annualElderValue:val});
      this.setData({annualElderChoice:elderChoice});
      return;
    }else if(val === 600){
      elderChoice.choice0 = false;
      elderChoice.choice1 = false;
      elderChoice.choice2 = false;
      elderChoice.choice3 = false;
      elderChoice.choice4 = false;
      elderChoice.choice5 = true;
      elderChoice.choice6 = false;
      elderChoice.choice7 = false;
      elderChoice.choice8 = false;
      elderChoice.choice9 = false;
      elderChoice.choice10 = false;
      this.setData({annualElderValue:val});
      this.setData({annualElderChoice:elderChoice});
      return;
    }else if(val === 500){
      elderChoice.choice0 = false;
      elderChoice.choice1 = false;
      elderChoice.choice2 = false;
      elderChoice.choice3 = false;
      elderChoice.choice4 = false;
      elderChoice.choice5 = false;
      elderChoice.choice6 = true;
      elderChoice.choice7 = false;
      elderChoice.choice8 = false;
      elderChoice.choice9 = false;
      elderChoice.choice10 = false;
      this.setData({annualElderValue:val});
      this.setData({annualElderChoice:elderChoice});
      return;
    }else if(val === 428.57){
      elderChoice.choice0 = false;
      elderChoice.choice1 = false;
      elderChoice.choice2 = false;
      elderChoice.choice3 = false;
      elderChoice.choice4 = false;
      elderChoice.choice5 = false;
      elderChoice.choice6 = false;
      elderChoice.choice7 = true;
      elderChoice.choice8 = false;
      elderChoice.choice9 = false;
      elderChoice.choice10 = false;
      this.setData({annualElderValue:val});
      this.setData({annualElderChoice:elderChoice});
      return;
    }
    else if(val === 375){
      elderChoice.choice0 = false;
      elderChoice.choice1 = false;
      elderChoice.choice2 = false;
      elderChoice.choice3 = false;
      elderChoice.choice4 = false;
      elderChoice.choice5 = false;
      elderChoice.choice6 = false;
      elderChoice.choice7 = false;
      elderChoice.choice8 = true;
      elderChoice.choice9 = false;
      elderChoice.choice10 = false;
      this.setData({annualElderValue:val});
      this.setData({annualElderChoice:elderChoice});
      return;
    }
    else if(val === 333.33){
      elderChoice.choice0 = false;
      elderChoice.choice1 = false;
      elderChoice.choice2 = false;
      elderChoice.choice3 = false;
      elderChoice.choice4 = false;
      elderChoice.choice5 = false;
      elderChoice.choice6 = false;
      elderChoice.choice7 = false;
      elderChoice.choice8 = false;
      elderChoice.choice9 = true;
      elderChoice.choice10 = false;
      this.setData({annualElderValue:val});
      this.setData({annualElderChoice:elderChoice});
      return;
    }
    else if(val === 300){
      elderChoice.choice0 = false;
      elderChoice.choice1 = false;
      elderChoice.choice2 = false;
      elderChoice.choice3 = false;
      elderChoice.choice4 = false;
      elderChoice.choice5 = false;
      elderChoice.choice6 = false;
      elderChoice.choice7 = false;
      elderChoice.choice8 = false;
      elderChoice.choice9 = false;
      elderChoice.choice10 = true;
      this.setData({annualElderValue:val});
      this.setData({annualElderChoice:elderChoice});
      return;
    }else{
      elderChoice.choice0 = true;
      elderChoice.choice1 = false;
      elderChoice.choice2 = false;
      elderChoice.choice3 = false;
      elderChoice.choice4 = false;
      elderChoice.choice5 = false;
      elderChoice.choice6 = false;
      elderChoice.choice7 = false;
      elderChoice.choice8 = false;
      elderChoice.choice9 = false;
      elderChoice.choice10 = false;
      this.setData({annualElderValue:val});
    this.setData({annualElderChoice:elderChoice});
    }
    return;
  },

  bindEduDateChange(e){
    var nowIdx = e.currentTarget.dataset.idx;//获取当前索引
    var val = e.detail.value;//获取输入的索引
    var oldVal = this.data.annualChildEduStart;
    let that = this;
    oldVal[nowIdx]=val;//修改对应索引值的内容
    that.setData({
      annualChildEduStart: oldVal
    });
  },

  bindEduGraduateDateChange(e){
    var nowIdx = e.currentTarget.dataset.idx;//获取当前索引
    var val = e.detail.value;
    var end = this.data.annualChildEduEnd;
    var start = this.data.annualChildEduStart;
    if(start[nowIdx].indexOf("-") === -1){
      wx.showToast({
        title: '请先输入子女入学时间！',
        icon:'none'
      });
      return;
    }
    let that = this;
    end[nowIdx] = val;
    that.setData({
      annualChildEduEnd: end
    });
  },
  

  bindRuxueDateChange(e){
    var val = e.detail.value;
    let that = this;
    that.setData({
      annualDegreeStart: val
    });
  },

  bindGraduateDateChange(e){
    var val = e.detail.value;
    var start = this.data.annualDegreeStart;
    if(start.indexOf("-") === -1){
      wx.showToast({
        title: '请先输入入学时间！',
        icon:'none'
      });
      return;
    }
    let that = this;
    that.setData({
      annualDegreeEnd: val
    });
  },

  bindLoanStartDateChange(e){
    var val = e.detail.value;
    let that = this;
    that.setData({
      annualLoanStartDate: val
    });
  },

  bindLoanEndDateChange(e){
    var val = e.detail.value;
    let that = this;
    var start = this.data.annualLoanStartDate;
    if(start.indexOf("-") === -1){
      wx.showToast({
        title: '请先输入首次还款时间！',
        icon:'none'
      });
      return;
    }
    that.setData({
      annualLoanEndDate: val
    });
  },

  bindElderDateChange(e){
    var val = e.detail.value;
    let that = this;
    that.setData({
      annualElderDate: val
    });
  },

  bindRentStartChange(e){ 
    var nowIdx = e.currentTarget.dataset.idx;//获取当前索引 
    var val = e.detail.value;//获取输入的索引 
    let that = this; 
    var rentStart = that.data.annualRentStart; 
    rentStart[nowIdx] = val; 
    that.setData({annualRentStart:rentStart}); 
  }, 
 
  bindRentEndChange(e){ 
    var nowIdx = e.currentTarget.dataset.idx;//获取当前索引 
    var val = e.detail.value;//获取输入的索引 
    var start = this.data.annualRentStart; 
    // 不选择开始时间，结束时间也不能选！ 
    if(start[nowIdx].indexOf("-") === -1){ 
      wx.showToast({ 
        title: '请选择房租开始时间！', 
        icon:'none' 
      }); 
      return; 
    } 
    var end = this.data.annualRentEnd; 
    end[nowIdx] = val; 
    this.setData({annualRentEnd:end}); 
  }, 

  bindBabyBirthChange(e){
    var nowIdx = e.currentTarget.dataset.idx;//获取当前索引 
    var val = e.detail.value;//获取输入的索引
    var birth = this.data.annualBabyBirth;
    birth[nowIdx] = val;
    this.setData({annualBabyBirth:birth});
    // console.log(this.data.componentCount);
  },
 
  
// 计算累计子女教育扣除金额
calChildEdu:function(){
  var result = 0,minus = 0;
  var ruxuedate = this.data.annualChildEduStart;
  var graduate = this.data.annualChildEduEnd;
  
  // console.log(ruxuedate);
  for(var i = 0; i < ruxuedate.length;i++){
    if(ruxuedate[i].indexOf("-") === -1 || graduate[i].indexOf("-") === -1){
      continue;
    }
    minus = this.calMonthInThisYear(ruxuedate[i],graduate[i]);
    console.log("minusMonthforchildEdu:"+minus);
    result = result + minus * 2000;
  }
  var percent = this.data.annualChildEduRatioValue;
  result = (result * Number(percent)).toFixed(2);
  console.log("result:"+result);
  this.setData({"componentCount.childEdu":result});
  console.log(this.data.componentCount)
},

calLoan:function(){
  var loanStart = this.data.annualLoanStartDate;
  var loanEnd = this.data.annualLoanEndDate;
  // 首套贷的时间区间和租房时间区间是一样的
  var minus = this.calMonthInThisYear(loanStart,loanEnd)
  var loanNum = this.data.annualLoanNum;
  var loan = minus * Number(loanNum);
  loan = loan.toFixed(2);
  this.setData({"componentCount.loan":loan});
},

//计算学历教育的专项附加扣除
calDegree:function(){
  var ruxueDate = this.data.annualDegreeStart;
  var graduate = this.data.annualDegreeEnd;
  var degreeEdu = this.data.annualDegreeValue;
  var minus = this.calMonthInThisYear(ruxueDate,graduate);
  var result = minus * degreeEdu;
  result = result.toFixed(2);
  let that = this;
  that.setData({"componentCount.degree":result});
},

// 计算扣除标准
calElder:function(){
  var elderDate = this.data.annualElderDate;
  var minus = this.calMonthMinus(elderDate);
  var ratio = this.data.annualElderValue;
  var result = minus * Number(ratio);
  this.setData({'componentCount.elder':result.toString()});
},

  
  // 计算婴幼儿照护部分的专项附加扣除
  calBabyCare:function(){
    var birth = this.data.annualBabyBirth;
    var monthMinus = 0,result = 0;
    for(var i = 0;i < birth.length;i++){
      monthMinus = this.calMonthMinus(birth[i]);
      result = result + monthMinus * 2000;
    }
    var ratio = this.data.annualBabyCareRatioValue;
    result = (result * ratio).toFixed(2);
    this.setData({"componentCount.babyCare":result});
  },

  //修改子女教育扣除主体
  radioEducationChange(e){
    // 记录用户的选择记录
     var newRatio = e.detail.value;
     var edu = this.data.annualChildEduRatioChoice;
     if(newRatio === '1'){
       edu.choiceWhole = true;
       edu.choiceHalf = false;
     }else{
       edu.choiceHalf = true;
       edu.choiceWhole = false;
     }
     this.setData({annualChildEduRatioChoice:edu});
     newRatio = Number.parseFloat(newRatio);
    //  var childEduFee = this.data.childNum * 2000 * newRatio;
     this.setData({annualBabyCareRatioValue:newRatio});
  },

  //继续教育专项附加扣除计算,只有2项
  furtherEducationChange(e){
    var furtherEduChoice = this.data.annualFurtherEduChoice;
    var furEdu = Number(e.detail.value);
    if(furEdu === 3600 && furDate.indexOf("-") === -1){
      wx.showToast({
        title: '请选择本年度申报继续教育的时间',
        icon:'none'
      });
      furtherEduChoice.yes = false;
      furtherEduChoice.no = true;
      this.setData({annualFurtherEduChoice:furtherEduChoice});
      return; 
    }
    if(furEdu === 3600){
      furtherEduChoice.yes = true;
      furtherEduChoice.no = false;
    }else{
      furtherEduChoice.yes = false;
      furtherEduChoice.no = true;
    }
    this.setData({"componentCount.furEdu":furEdu,annualFurtherEduChoice:furtherEduChoice});
    // var result = this.calSumResult();
    // this.setData({totalCount:result});
  },

  degreeEducationChange(e){
    var degreeEdu = Number(e.detail.value);
    var ruxueDate = this.data.annualDegreeStart;
    var graduateDate = this.data.annualDegreeEnd;
    var degreeChoice = this.data.annualDegreeChoice;
    // 申报了学历教育但是不填写入学时间会有提示
    if(degreeEdu === 400 && (ruxueDate.indexOf("-") === -1 || graduateDate.indexOf("-") === -1)){
      wx.showToast({
        title: '请输入入学与毕业时间！\r\n否则学历教育填报无效',
        icon:'none'
      });
      degreeChoice.yes=false;
      degreeChoice.no=true;
      this.setData({annualDegreeChoice:degreeChoice});
      return;
    }
    if(degreeEdu === 400){
      degreeChoice.yes=true;
      degreeChoice.no=false;
      this.setData({annualDegreeChoice:degreeChoice});
    }else{
      degreeChoice.yes=false;
      degreeChoice.no=true;
      this.setData({annualDegreeChoice:degreeChoice});
    }
    this.setData({annualDegreeValue:degreeEdu});
  },

  //首套房房贷处理
  loanChange(e){
    var loanStartDate = this.data.annualLoanStartDate;
    var loanEndDate = this.data.annualLoanEndDate;
    var loanChoice = this.data.annualLoanChoice;
    var loanNum = Number(e.detail.value);
    this.setData({annualLoanNum:loanNum});
    // 填写贷款信息但是没有填写时间
    if(loanNum !== 0 && (loanStartDate.indexOf("-") === -1 || loanEndDate.indexOf("-") === 1)){
      wx.showToast({
        title: '请输入首套房相关时间信息！\r\n否则填报信息无效',
        icon:'none'
      });
      loanChoice.half = false;
      loanChoice.whole = false;
      loanChoice.noneLoan = true;
      this.setData({annualLoanChoice:loanChoice});
      return;
    }
    if(loanNum === 500){
      loanChoice.half = true;
      loanChoice.whole = false;
      loanChoice.noneLoan = false;
    }else if(loanNum === 1000){
      loanChoice.half = false;
      loanChoice.whole = true;
      loanChoice.noneLoan = false;
    }else{
      loanChoice.half = false;
      loanChoice.whole = false;
      loanChoice.noneLoan = true;
    }
    this.setData({loanChannualLoanChoiceoice:loanChoice});
  },

  //住房租金城市处理
  onRentChange(e){
    var nowIdx = e.currentTarget.dataset.idx;//获取当前索引
    var start = this.data.annualRentStart;
    var end = this.data.annualRentEnd;
    var loanNum = Number(e.detail.value);
    var rent = this.data.annualRentType;
    var rentChoice = this.data.annualRentChoice;
    rent[nowIdx] = loanNum;
    // console.log("start:"+start[nowIdx]+" end:"+end[nowIdx]);
    // console.log("start判断结果"+start[nowIdx].includes("-"))
    if(start[nowIdx].includes("-") === false || end[nowIdx].includes("-") === false){
      wx.showToast({
        title: '请完整填写租房时间！',
        icon:'none'
      });
      rentChoice[nowIdx].noneCity = true;
      rentChoice[nowIdx].cityA = false;
      rentChoice[nowIdx].cityB = false;
      rentChoice[nowIdx].cityC = false;
      this.setData({annualRentChoice:rentChoice})
      return;
    }
    if(loanNum === 0){
      rentChoice[nowIdx].noneCity = true;
      rentChoice[nowIdx].cityA = false;
      rentChoice[nowIdx].cityB = false;
      rentChoice[nowIdx].cityC = false;
    }else if (loanNum === 800){
      rentChoice[nowIdx].noneCity = false;
      rentChoice[nowIdx].cityA = false;
      rentChoice[nowIdx].cityB = false;
      rentChoice[nowIdx].cityC = true;
    }else if (loanNum === 1100){
      rentChoice[nowIdx].noneCity = false;
      rentChoice[nowIdx].cityA = false;
      rentChoice[nowIdx].cityB = true;
      rentChoice[nowIdx].cityC = false;
    }else{
      rentChoice[nowIdx].noneCity = false;
      rentChoice[nowIdx].cityA = true;
      rentChoice[nowIdx].cityB = false;
      rentChoice[nowIdx].cityC = false;
    }
    this.setData({annualRentChoice:rentChoice})
    this.setData({annualRentType:rent});
  },

  //选择婴幼儿扣除主体
  ratioBabyCare(e){
     var newRatio = e.detail.value;
     var babyChoice = this.data.annualBabyChoice;
     newRatio = Number.parseFloat(newRatio);
     if(newRatio === 0.5){
      babyChoice.choiceHalf = true;
      babyChoice.choiceWhole = false;
     }else{
      babyChoice.choiceHalf = false;
      babyChoice.choiceWhole = true;
     }
     this.setData({annualBabyCareRatioValue:newRatio,annualBabyChoice:babyChoice});
  },

  formSubmit: function(event) {
    var that = this
    that.calChildEdu();
    that.calDegree();
    that.calLoan();
    that.calRentKouchu();
    that.calBabyCare();
    that.calElder();
    that.calSumResult();

    const annualChildEduBox=JSON.stringify(that.data.annualChildEduBox)
    const annualChildEduStart=JSON.stringify(that.data.annualChildEduStart)
    const annualChildEduEnd=JSON.stringify(that.data.annualChildEduEnd)
    const annualChildEduRatioChoice=JSON.stringify(that.data.annualChildEduRatioChoice)
    const annualChildEduRatioValue=JSON.stringify(that.data.annualChildEduRatioValue)
    
    const annualFurtherEduChoice=JSON.stringify(that.data.annualFurtherEduChoice)
    const annualDegreeChoice=JSON.stringify(that.data.annualDegreeChoice)
    const annualDegreeStart=JSON.stringify(that.data.annualDegreeStart)
    const annualDegreeEnd=JSON.stringify(that.data.annualDegreeEnd)
    const annualDegreeValue=JSON.stringify(that.data.annualDegreeValue)
    const annualBabyBox=JSON.stringify(that.data.annualBabyBox)
    const annualBabyBirth=JSON.stringify(that.data.annualBabyBirth)
    const annualBabyCareRatioValue=JSON.stringify(that.data.annualBabyCareRatioValue)
    const annualBabyChoice=JSON.stringify(that.data.annualBabyChoice)
    const annualRentBox=JSON.stringify(that.data.annualRentBox)
    const annualRentStart=JSON.stringify(that.data.annualRentStart)
    const annualRentEnd=JSON.stringify(that.data.annualRentEnd)
    const annualRentType=JSON.stringify(that.data.annualRentType)
    const annualRentChoice=JSON.stringify(that.data.annualRentChoice)
    const annualLoanNum=JSON.stringify(that.data.annualLoanNum)
    const annualLoanChoice=JSON.stringify(that.data.annualLoanChoice)
    const annualLoanEndDate=JSON.stringify(that.data.annualLoanEndDate)
    const annualLoanStartDate=JSON.stringify(that.data.annualLoanStartDate)
    const annualElderDate=JSON.stringify(that.data.annualElderDate)
    const annualElderChoice=JSON.stringify(that.data.annualElderChoice)
    const annualElderValue=JSON.stringify(that.data.annualElderValue)
    
    try {
      wx.setStorageSync('annualChildEduBox',        annualChildEduBox)
      wx.setStorageSync('annualChildEduStart',      annualChildEduStart)
      wx.setStorageSync('annualChildEduEnd',        annualChildEduEnd)
      wx.setStorageSync('annualChildEduRatioChoice',annualChildEduRatioChoice)
      wx.setStorageSync('annualChildEduRatioValue', annualChildEduRatioValue)
    
      wx.setStorageSync('annualFurtherEduChoice',   annualFurtherEduChoice)
      wx.setStorageSync('annualDegreeChoice',       annualDegreeChoice)
      wx.setStorageSync('annualDegreeStart',        annualDegreeStart)
      wx.setStorageSync('annualDegreeEnd',          annualDegreeEnd)
      wx.setStorageSync('annualDegreeValue',        annualDegreeValue)
      wx.setStorageSync('annualBabyBox',            annualBabyBox)
      wx.setStorageSync('annualBabyBirth',          annualBabyBirth)
      wx.setStorageSync('annualBabyCareRatioValue', annualBabyCareRatioValue)
      wx.setStorageSync('annualBabyChoice',         annualBabyChoice)
      wx.setStorageSync('annualRentBox',            annualRentBox)
      wx.setStorageSync('annualRentStart',          annualRentStart)
      wx.setStorageSync('annualRentEnd',            annualRentEnd)
      wx.setStorageSync('annualRentType',           annualRentType)
      wx.setStorageSync('annualRentChoice',         annualRentChoice)
      wx.setStorageSync('annualLoanNum',            annualLoanNum)
      wx.setStorageSync('annualLoanChoice',         annualLoanChoice)
      wx.setStorageSync('annualLoanEndDate',        annualLoanEndDate)
      wx.setStorageSync('annualLoanStartDate',      annualLoanStartDate)
      wx.setStorageSync('annualElderDate',          annualElderDate)
      wx.setStorageSync('annualElderChoice',        annualElderChoice)
      wx.setStorageSync('annualElderValue',         annualElderValue)
    } catch (e) { 
      console.log(e)
    }
    
    /**
     
      const graduateDate = JSON.stringify(this.data.graduateDate)
     const loanEndDate = JSON.stringify(this.data.loanEndDate)
     */
    let pages = getCurrentPages();//当前页面
    let prevPage = pages[ pages.length - 2 ];
    var value = that.data.totalAnnualPersonCount;
    //app.globalData.zxPerson = that.data.totalCount;
    prevPage.setData({
      annualZhuanXiang:value
    });
    wx.navigateBack({
      delta: 1  // 返回上一级页面。
    });
  },

  formReset:function(){
    //console.log('已清空填写内容')
    this.setData({
      annualChildEduBox:[1],
      annualChildEduStart:['子女入学时间'],
      annualChildEduEnd:['终止教育时间'],
      annualChildEduRatioChoice:{choiceHalf:true,choiceWhole:false},
      annualChildEduRatioValue:0.5,
      
      annualFurtherEduChoice:{yes:false,no:true},
      annualDegreeChoice:{yes:false,no:true},
      annualDegreeStart:"请选择入学时间",
      annualDegreeEnd:"预计毕业时间",
      annualDegreeValue:0,
      annualBabyBox:[1],
      annualBabyBirth:['请选择出生年月'],
      annualBabyCareRatioValue:0.5,
      annualBabyChoice:{choiceHalf:true,choiceWhole:false},
      annualRentBox:[], 
      annualRentStart:['房租开始时间'], 
      annualRentEnd:['房租到期时间'], 
      annualRentType:[], 
      annualRentChoice:[{cityA:false,cityB:false,cityC:false,noneCity:true}],  
      annualLoanNum:0,
      annualLoanChoice:{half:false,whole:false,noneLoan:true},
      annualLoanEndDate:"贷款还完时间",
      annualLoanStartDate:"首次还款时间",        
      annualElderDate:"请选择老人何时年满六十",
      annualElderChoice:{choice1:false,choice2:false,choice3:false,choice4:false,choice5:false,
        choice6:false,choice7:false,choice8:false,choice9:false,choice10:false,choice0:true},
        annualElderValue:0,
      totalAnnualPersonCount:'0.00',
    });
},
  
  onLoad(options) {
    wx.showShareMenu({
      withShareTicket:true,
      menus:["shareAppMessage",'shareTimeline']});
    const mon = options.calYear; 
    this.setData({nowYear:mon}); 
    // console.log(mon);

    //根据用户输入自动算出婴幼儿最早的出生年月 
    var year = Number(mon) - 3; 
    year = year + "-01"; 
    this.setData({annualBabyBond:year}); 

    let that = this;
    try {
      var value = wx.getStorageSync('annualChildEduBox')
      if(value){
      that.setData({annualChildEduBox:JSON.parse(value)});
      }   
      value = wx.getStorageSync('annualChildEduStart')
      if(value){
      that.setData({annualChildEduStart:JSON.parse(value)});
      }     
      value = wx.getStorageSync('annualChildEduEnd')
      if(value){
      that.setData({annualChildEduEnd:JSON.parse(value)});
      }       
      value = wx.getStorageSync('annualChildEduRatioChoice')
      if(value){
        that.setData({annualChildEduRatioChoice:JSON.parse(value)});
      }
      value = wx.getStorageSync('annualChildEduRatioValue')
      if(value){
      that.setData({annualChildEduRatioValue:JSON.parse(value)});
      }
      value = wx.getStorageSync('annualFurtherEduChoice')
      if(value){
      that.setData({annualFurtherEduChoice:JSON.parse(value)});
      }  
      value = wx.getStorageSync('annualDegreeChoice')
      if(value){
      that.setData({annualDegreeChoice:JSON.parse(value)});
      }      
      value = wx.getStorageSync('annualDegreeStart')
      if(value){
      that.setData({annualDegreeStart:JSON.parse(value)});
      }       
      value = wx.getStorageSync('annualDegreeEnd')
      if(value){
      that.setData({annualDegreeEnd:JSON.parse(value)});
      }         
      value = wx.getStorageSync('annualDegreeValue')
      if(value){
      that.setData({annualDegreeValue:JSON.parse(value)});
      }       
      value = wx.getStorageSync('annualBabyBox')
      if(value){
      that.setData({annualBabyBox:JSON.parse(value)});
      }           
      value = wx.getStorageSync('annualBabyBirth')
      if(value){
      that.setData({annualBabyBirth:JSON.parse(value)});
      }         
      value = wx.getStorageSync('annualBabyCareRatioValue')
      if(value){
      that.setData({annualBabyCareRatioValue:JSON.parse(value)});
      }
      value = wx.getStorageSync('annualBabyChoice')
      if(value){
      that.setData({annualBabyChoice:JSON.parse(value)});
      }        
      value = wx.getStorageSync('annualRentBox')
      if(value){
      that.setData({annualRentBox:JSON.parse(value)});
      }           
      value = wx.getStorageSync('annualRentStart')
      if(value){
      that.setData({annualRentStart:JSON.parse(value)});
      }         
      value = wx.getStorageSync('annualRentEnd')
      if(value){
      that.setData({annualRentEnd:JSON.parse(value)});
      }           
      value = wx.getStorageSync('annualRentType')
      if(value){
      that.setData({annualRentType:JSON.parse(value)});
      }          
      value = wx.getStorageSync('annualRentChoice')
      if(value){
      that.setData({annualRentChoice:JSON.parse(value)});
      }        
      value = wx.getStorageSync('annualLoanNum')
      if(value){
      that.setData({annualLoanNum:JSON.parse(value)});
      }           
      value = wx.getStorageSync('annualLoanChoice')
      if(value){
      that.setData({annualLoanChoice:JSON.parse(value)});
      }        
      value = wx.getStorageSync('annualLoanEndDate')
      if(value){
      that.setData({annualLoanEndDate:JSON.parse(value)});
      }       
      value = wx.getStorageSync('annualLoanStartDate')
      if(value){
      that.setData({annualLoanStartDate:JSON.parse(value)});
      }     
      value = wx.getStorageSync('annualElderDate')
      if(value){
      that.setData({annualElderDate:JSON.parse(value)});
      }         
      value = wx.getStorageSync('annualElderChoice')
      if(value){
      that.setData({annualElderChoice:JSON.parse(value)});
      }       
      value = wx.getStorageSync('annualElderValue')
      if(value){
      that.setData({annualElderValue:JSON.parse(value)});
      }        
    } catch (e) {
      // Do something when catch error
      console.log(e)
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },
  

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    return {
      title: '答哥算税计算器',
      path: '/pages/index/index'
    }
  }
})